<?php
class EditAlbumCtl extends BaseCtl {
	public function EditAlbumCtl() {
		parent::BaseCtl();
	}
	
    public function isPostback(){
    	return 'editalbum' == $this->getPost('pagename');
    }

    public function loadData(){
    	$form = $this->getEditAlbumForm();
    	
    	$form->setId($this->getPost('id'));
    	$form->setAdding('yes' == $this->getPost('adding'));
    	$form->setName($this->getPost('name'));
    }
	
    public function submit(){
		try {
			$form = $this->getEditAlbumForm();
			$frm = $this->getAlbumsForm();
			
			$adding = $form->isAdding();
			$id = $form->getId();
			$name = $form->getName();
			
			$service = new Service();
			if($adding){
				$userId = HtmlUtil::getSessionUser()->getUser()->getId();
				$seqNo = $service->getMaximumAlbumSeq() + 10;
				 
				$service->addAlbum($id, $name, $userId, $seqNo);
				$frm->addAlbum($id, $name, $userId, $seqNo);
			}
			else {
				$service->updateAlbum($id, $name);
				$frm->updateAlbum($id, $name);
			}
			
			$this->redirect('albums.php');
		}
		catch(Exception $e){
			HtmlUtil::addError($e->getMessage());
		}
	}
	
	public function cancel(){
		try {
			$this->redirect('albums.php');
		}
		catch(Exception $e){
			HtmlUtil::addError($e->getMessage());
		}
	}
} 
?>